package cn.ranko.shiro.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.ranko.bean.ComboBox;
import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.bean.authority.Message;
import cn.ranko.shiro.constants.ApplicationCode;
import cn.ranko.shiro.model.SysClass;
import cn.ranko.shiro.model.SysRole;
import cn.ranko.shiro.service.SysClassService;

@Controller
@RequestMapping("/shiro/class")
public class ClassController {
	
	@Autowired
	SysClassService sysClassService;
 
    @RequiresPermissions(value = {"class_add","class_edit"},logical = Logical.OR)
    @RequestMapping("/showedit.action")
    public String showedit(Integer classId,ModelMap modelMap){
        if(classId ==0){
            //添加
        }else{
            //修改
            SysClass sysClass = sysClassService.getBySysClassId(classId);
            modelMap.addAttribute("sysClass",sysClass);
        }
        return "class/showedit";
    }

    
    
    @RequestMapping("/loadComboBoxData.action")
    @ResponseBody
    public List<ComboBox> loadComboBoxData(HttpServletRequest request) {
        List<ComboBox> comboBoxes = new ArrayList<>();
        List<SysClass> querySysClassAll = sysClassService.getQuerySysClassAll();
        for (SysClass sysClass : querySysClassAll) {
        	  ComboBox comboBox = new ComboBox();
              comboBox.setId(sysClass.getClassId());
              comboBox.setText(sysClass.getClassName());
              comboBoxes.add(comboBox);
		}
        return comboBoxes;
    }

    
    @RequiresPermissions(value = {"class_add","class_edit"},logical = Logical.OR)
    @RequestMapping("/save.action")
    @ResponseBody
    public String save(SysClass sysClass,String roleIds){
        int affect = sysClassService.save(sysClass,roleIds);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }
    
    @RequiresPermissions("class_delete")
    @RequestMapping("/remove.action")
    @ResponseBody
    public String remove(Integer classId){
        if(sysClassService.delete(classId)>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else{
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }
//    
//    @RequestMapping("/view.action")
//    public String view(Integer classId,ModelMap modelMap){
//
//        SysPicklist sysPicklist = sysPicklistService.getByPicklistId(picklist_id);
//        modelMap.addAttribute("sysPicklist",sysPicklist);
//
//        List<SysPicklistvalue> sysPicklistvalues = sysPicklistvalueService.getByPicklistId(picklist_id);
//        modelMap.addAttribute("sysPicklistvalues",sysPicklistvalues);
//
//        return "picklist/view";
//    }
    
    @RequiresPermissions("class_manager")
    @RequestMapping("/loadList.action")
    @ResponseBody
    public Datagrid loadList(Integer page, Integer rows){
        Pagination pagination = new Pagination();
        pagination.setPage(page);
        pagination.setRows(rows);
        return sysClassService.loadList(pagination);
    }
    
    
    @RequiresPermissions("classManager")
    @RequestMapping("/classManager.action")
    public String classManager() {
    	
        return "class/classManager";
    }
}
